1 package org.apache.maven.surefire.junitcore.pc; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.junit.runners.model.RunnerScheduler; 23 24 import java.util.concurrent.RejectedExecutionException; 25 26 /** 27 * Specifies the strategy of scheduling whether sequential, or parallel. 28 * The strategy may use a thread pool <em>shared</em> with other strategies. 29 * <p/> 30 * One instance of strategy can be used just by one {@link Scheduler}. 31 * <p/> 32 * The strategy is scheduling tasks in {@link #schedule(Runnable)} and awaiting them 33 * completed in {@link #finished()}. Both methods should be used in one thread. 34 * 35 * @author Tibor Digana (tibor17) 36 * @since 2.16 37 */ 38 public abstract class SchedulingStrategy 39 { 40 41 /** 42 * Schedules tasks if {@link #canSchedule()}. 43 * 44 * @param task runnable to schedule in a thread pool or invoke 45 * @throws RejectedExecutionException if <tt>task</tt> 46 * cannot be scheduled for execution 47 * @throws NullPointerException if <tt>task</tt> is <tt>null</tt> 48 * @see RunnerScheduler#schedule(Runnable) 49 * @see java.util.concurrent.Executor#execute(Runnable) 50 */ 51 protected abstract void schedule( Runnable task ); 52 53 /** 54 * Waiting for scheduled tasks to finish. 55 * New tasks will not be scheduled by calling this method. 56 * 57 * @return <tt>true</tt> if successfully stopped the scheduler, else 58 * <tt>false</tt> if already stopped (a <em>shared</em> thread 59 * pool was shutdown externally). 60 * @throws InterruptedException if interrupted while waiting 61 * for scheduled tasks to finish 62 * @see RunnerScheduler#finished() 63 */ 64 protected abstract boolean finished() 65 throws InterruptedException; 66 67 /** 68 * Stops scheduling new tasks (e.g. by {@link java.util.concurrent.ExecutorService#shutdown()} 69 * on a private thread pool which cannot be <em>shared</em> with other strategy). 70 * 71 * @return <tt>true</tt> if successfully stopped the scheduler, else 72 * <tt>false</tt> if already stopped (a <em>shared</em> thread 73 * pool was shutdown externally). 74 * @see java.util.concurrent.ExecutorService#shutdown() 75 */ 76 protected abstract boolean stop(); 77 78 /** 79 * Stops scheduling new tasks and <em>interrupts</em> running tasks 80 * (e.g. by {@link java.util.concurrent.ExecutorService#shutdownNow()} on a private thread pool 81 * which cannot be <em>shared</em> with other strategy). 82 * <p/> 83 * This method calls {@link #stop()} by default. 84 * 85 * @return <tt>true</tt> if successfully stopped the scheduler, else 86 * <tt>false</tt> if already stopped (a <em>shared</em> thread 87 * pool was shutdown externally). 88 * @see java.util.concurrent.ExecutorService#shutdownNow() 89 */ 90 protected boolean stopNow() 91 { 92 return stop(); 93 } 94 95 protected void setDefaultShutdownHandler( Scheduler.ShutdownHandler handler ) 96 { 97 } 98 99 /** 100 * @return <tt>true</tt> if a thread pool associated with this strategy 101 * can be shared with other strategies. 102 */ 103 protected abstract boolean hasSharedThreadPool(); 104 105 /** 106 * @return <tt>true</tt> unless stopped or finished. 107 */ 108 protected abstract boolean canSchedule(); 109 }